import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;


public class QandASystem {
	
	public static Index index;
	private Vector<String> keywords;
	private String productName;
	
	public QandASystem() {
		index = new Index();
		keywords = new Vector<String>();
	}

	
	/**
	 * This method allows to enter the question, it will then
	 * parser it, stem it and remove the stop words, and finally
	 * extract the keywords
	 * @param request
	 */
	public void enterQuestion(String request) {
		System.out.println("#Processing of the question#");
		String question = new String(request);
		
		//force lowercase
		question = question.toLowerCase();
		
		//parse the question into a vector of strings
		Vector<String> words = Parser.parse(question);
		
		//####NOT CASE#####
		
		//stem each words
		words = Stemmer.stem(words);
		
		//remove stop words
		words = StopWordRemover.removeStopWords(words);
		
		keywords = words;
		System.out.println("Extracted keywords: "+keywords);
		System.out.println("");
	}
	
	/**
	 * This method returns the result of the rating process.
	 * 
	 * @param keywords		The keywords extracted from the question
	 * @param productName	The name of the product
	 * @return				An RatingResult object with the results of the rating process
	 */
	//Get the relevant sentences and comments
	private RatingResult retrieveResult(Vector<String> keywords, String productName)
	{
		CommentRater cr = new CommentRater();
		return cr.rateComments(keywords,productName);
	}
	
	/**
	 * Process the obtained results, displaying the results depending on the operation mode
	 * (question about a single product, or all products)
	 * 
	 * @param r		The result of the rating process
	 * @param mode	The operation mode (single product or all products)
	 */
	//PROCESS THE RETRIEVED SENTENCES
	public void processAnswer(RatingResult r, String mode)
	{
		if(mode.equals("single"))
		{
			System.out.println("Number of POS sentences: "+r.getGoodSentences().size());
			System.out.println("Number of NEG sentences: "+r.getBadSentences().size());
			System.out.println("Number of NEU sentences: "+r.getNeutralSentences().size());

			r.displayGoodSentences(5);
			System.out.println("");
			r.displayBadSentences(5);
			System.out.println("");
			r.displayNeutralSentences(3);
			System.out.println("");
		}
		else
		{
			System.out.println("Number of POS sentences: "+r.getGoodSentences().size());
			System.out.println("Number of NEG sentences: "+r.getBadSentences().size());
			System.out.println("Number of NEU sentences: "+r.getNeutralSentences().size());

			r.displayGoodSentences(5);
			System.out.println("");
			r.displayBadSentences(3);
			System.out.println("");
			r.displayNeutralSentences(1);
			System.out.println("");

			System.out.println("Recommended products:");
			r.displayBestProducts(3);
		}
		
	}
	
	
	//############ GETTERS AND SETTERS##############################
	public Vector<String> getKeywords() {
		return keywords;
	}

	public void setKeywords(Vector<String> keywords) {
		this.keywords = keywords;
	}
	//############ END GETTERS AND SETTERS #########################



	// ###################### MAIN #################################
	public static void main(String[] args) throws IOException {

		QandASystem m = new QandASystem();

		while(true)
		{
			System.out.println("######################################");
			System.out.println("###### QUESTION & ANSWER SYSTEM ######");
			System.out.println("######################################");
			System.out.println("");

			System.out.println("Choose the type of question you want:");
			System.out.println(" 1 - Question about all the products");
			System.out.println(" 2 - Question about a single product");
			System.out.println("Enter 1 or 2");
			String choice = new BufferedReader(new InputStreamReader(System.in)).readLine();
			
			
			if(choice.equals("2"))
			{
				// QUESTIONS ABOUT A SINGLE PRODUCT
				
				System.out.println("#Complete the question below#");
				//Ideally, we should choose the product here
				m.productName="Cuisinart-DCC-1200-Central-Coffeemaker-Stainless";
				System.out.print("Is/Does this coffee machine ");

				String x = new BufferedReader(new InputStreamReader(System.in)).readLine();
				System.out.println("");

				m.enterQuestion(x);

				for(String key : m.getKeywords())
				{
					System.out.println("Keyword: "+key);
					Vector<String> syn = WordNet.getSynonyms(key);
					System.out.println("Synonyms: "+syn);
					Vector<String> ant = WordNet.getAntonyms(key);
					System.out.println("Antonyms: "+ant);
				}
				System.out.println("");

				RatingResult r = m.retrieveResult(m.getKeywords(),m.productName);

				m.processAnswer(r,"single");
			}
			
			
			else
			{
				// QUESTIONS ABOUT ALL THE PRODUCTS
				m.productName = "all";
				System.out.println("#Complete the question below#");
				System.out.print("Which coffee machine ");

				String x = new BufferedReader(new InputStreamReader(System.in)).readLine();
				System.out.println("");

				m.enterQuestion(x);

				for(String key : m.getKeywords())
				{
					System.out.println("Keyword: "+key);
					Vector<String> syn = WordNet.getSynonyms(key);
					System.out.println("Synonyms: "+syn);
					Vector<String> ant = WordNet.getAntonyms(key);
					System.out.println("Antonyms: "+ant);
				}
				System.out.println("");

				RatingResult r = m.retrieveResult(m.getKeywords(),m.productName);

				m.processAnswer(r,"all");
				
			}


			//TO CONTINUE OR QUIT############################
			System.out.println("");
			System.out.println("To enter a new question, enter any key. To quit, enter q.");
			String x2 = new BufferedReader(new InputStreamReader(System.in)).readLine();
			if(x2.equals("q"))
				break;
			else;
			//###############################################"
		}
	}

}
